package cli

import (
	"blockchain.com/part_5/wallet"
	"blockchain.com/part_5/block"
	"blockchain.com/part_5/utils"
	"log"
	"fmt"
)

func (cli *CLI)GetBalance(address string)  {
	if !wallet.ValidateAddress(address) {
		log.Panic("ERROR: Address is not valid.")
	}

	// 从数据库中获取区块链
	bc := block.NewBlockchain(address)
	defer bc.Db.Close()
	// 余额：账户余额就是由账户地址锁定的所有未花费交易输出的总和
	balance := 0
	// 获取交易输出数据
	pubKeyHash := utils.Base58Decode([]byte(address))
	pubKeyHash = pubKeyHash[1:len(pubKeyHash)-4]
	UTXOs := bc.FindUTXO(pubKeyHash)
	// 遍历列表
	for _,out := range UTXOs{
		balance += out.Value
	}

	fmt.Printf("Balance of '%s':%d\n",address,balance)
}
